package main

import (
	"fmt"
	"log"
	"net/http"
	"os/exec"

	"github.com/gin-gonic/gin"
)

type Cmd struct {
	Script  string
	Content string
	Params  map[string]string
}

func main() {
	engine := gin.Default()

	route := engine.Group("")
	route.POST("/api/v1/robot", RPA)

	engine.Run(":9911")
}

func RPA(ctx *gin.Context) {
	var cmd Cmd
	if err := ctx.Bind(&cmd); err != nil {
		ctx.JSON(http.StatusBadRequest, err.Error())
		return
	}

	robot := "robot "
	// robot脚本参数拼接
	for key, value := range cmd.Params {
		robot += fmt.Sprintf("--variable %s:%s ", key, value)
	}
	// robot脚本目录拼接
	robot += fmt.Sprintf("D:\\Project\\robot\\WebDemo\\%s", cmd.Script)

	command := exec.Command("cmd", "/C", robot)
	log.Println(command)

	if output, err := command.CombinedOutput(); err == nil {
		ctx.JSON(http.StatusOK, string(output))
	} else {
		ctx.JSON(http.StatusBadRequest, err.Error())
	}
}
